<?php
/**
 * Created by PhpStorm.
 * User: wuchao
 * Date: 2020/4/15
 * Time: 10:48
 */

namespace app\api\model;

use \Exception as E;
use think\facade\Db;
use \think\response\Json;

class CateGoryModel
{
    private static $_table = 'category'; // 分类表
    private static $goods = 'goods'; //
//    private static $_dirverTable = 'dirver'; // 司机表
//    private static $_dirver_routeTable = 'dirver_route'; // 司机路由表

    /**获取一级分类
     * @param array $input
     * @return array
     */
    public static function GetCategory($input)
    {
        $data = Db::table(self::$_table)->where('admin_id', $input['admin_id'])->where('level', 0)->where('status', 1)->field('id,pic,name')->select()->toArray();
        return $data;
    }


    /**获取一级分类
     * @param array $input
     * @return Json
     */
    public static function OneCategory($input)
    {
        try {
            $map = [
                ['admin_id', '=', $input['admin_id']],
                ['level', '=', 0],
                ['status', '=', 1]
            ];
            $data = Db::table(self::$_table)->where($map)->field('id,pic,name')->select()->toArray();
            if ($data) return jsonSuccess('获取成功', $data);
            return jsonFail('未找到数据');
        } catch (E $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /**获取二级分类
     * @param array $input
     * @return Json
     */
    public static function TwoCategory($input)
    {
        try {
            $map = [
                ['admin_id', '=', $input['admin_id']],
                ['pid','=', $input['id']],
                ['level', '=', 1],
                ['status', '=', 1]
            ];
            $data = Db::table(self::$_table)->where($map)->field('id,pic,name')->select()->toArray();
            if ($data) return jsonSuccess('获取成功', $data);
            return jsonFail('未找到数据');
        } catch (E $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /**获取三级分类和商品
     * @param array $input
     * @return Json
     */
    public static function ThreeCategory($input)
    {
        try {
            $center_ids = explode(',',$input['center_ids']);
            if(empty($center_ids)) return jsonFail('分拣中心数据错误');
            $field = 'a.id,a.pic,a.name,b.id as goods_id,b.name as goods_name,b.sort,b.info as goods_info,b.image,b.price,b.ot_price,b.act_price,b.is_activity,b.sales,b.stock';
            $map = [
                ['a.admin_id', '=', $input['admin_id']],
                ['a.pid', '=', $input['id']],
                ['b.center_id', 'in', $center_ids],
                ['a.status', '=', 1],
                ['b.status', '=', 1],
            ];
            $data = Db::table(self::$_table)->alias('a')
                ->leftJoin([self::$goods => 'b'], 'a.id = b.cate_id')
                ->where($map)
                ->field($field)->select()->toArray();
            $newData = self::GetRepeatValGroup($data, 'id',$input);//TODO 组合数据
            if ($newData) {
                return jsonSuccess('获取成功', $newData);
            } else {
                return jsonFail('没有找到数据');
            }
        } catch (E $e) {
            return jsonFail('服务内部错误~');
        }

    }

    /**组合数据，相同的分类为二维，商品为三维
     * @param $arr
     * @param $keys
     * @param $input
     * @return array|bool
     */
    public static function GetRepeatValGroup($arr, $keys,$input)
    {
        if (!is_array($arr) || !$keys) {
            return false;
        }
        $temp = array();
        foreach ($arr as $key => &$val) {
            $temp[$val[$keys]]['id'] = $val['id'];
            $temp[$val[$keys]]['name'] = $val['name'];
            $temp[$val[$keys]]['pic'] = $val['pic'];
            $val['cate_num'] = Db::table('cart')->where('group_id',$input['group_id'])->where('goods_id',$val['goods_id'])->field('num,id as cart_id')->find();
            if (empty($val['goods_name'])) {
                unset($val['goods_name']);
                unset($val['goods_id']);
                unset($val['goods_info']);
                unset($val['image']);
                unset($val['ot_price']);
                unset($val['price']);
                unset($val['sales']);
                unset($val['stock']);
            } else {
                $temp[$val[$keys]]['goods'][] = $val;
                foreach ($temp[$val[$keys]]['goods'] as &$value) {
                    unset($value['id']);
                    unset($value['name']);
                    unset($value['pic']);
                }
            }
        }
        return $temp;
    }

    /**获取一级下所有三级分类
     * @param array $input
     * @return Json
     */
    public static function GetAllCategory($input)
    {
        $second = Db::table(self::$_table)->where('pid', $input['cate_id'])->field('id,pid')->select()->toArray();
        $second_ids = array_column($second, 'id');
        $three = Db::table(self::$_table)->where('pid', 'in', $second_ids)->field('id')->select()->toArray();
        return $three_ids = array_column($three, 'id');
    }

    //curl进行Https Get访问链接
    public static function curl_url($url)
    {
        $ch = curl_init();
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        $output = curl_exec($ch); //执行并获取HTML文档内容
        $data = json_decode($output);
        //释放curl句柄
        curl_close($ch);
        return $data;
    }
}